#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#define file_path_name "test_write.bin"
#define file_path_out "test_read.bin"

typedef struct _src_file_info
{
    char file_name[128];
    struct stat stat_info;
    FILE * fp;
}src_file_info;

struct stat file_stat;

src_file_info srcFileInfo[4];


void * stat_file_thread(void * param);
int flag_exit = 0;


char file_out_name[128];


void handler(int no)
{
   printf("received signal:%d\n",no);
   if(no==15)
   {
       flag_exit = 1;
   }
}

int getFileName(char *filename){
	
	FILE *fp;
	fp = fopen("filenameTemp.txt","rb+");
  	while(fp==NULL){
  		usleep(30);
  		fp = fopen("filenameTemp.txt","rb+");
  	} 
  	
	fscanf(fp,"%[^\n]", filename);
	
	//fread(filename,sizeof(char)*18,1,fp);
	//	printf("filename %s\n",filename);
	//		fclose(fp);
	//			return 1;;
	//			}
	//
}


int main(int argc, char * argv[])
{
//    int fp_read = open(file_path_name,O_RDONLY,S_IRWXO| S_IRWXG| S_IRUSR); 
//

    char char_get = 0;
    char file_name[128];
    getFileName(file_name);

    const char * name_arr[4] = {
    "lvx",
    "gps",
    "lpms",
    "pps"
    };
    for(int i=0;i<4;i++)
    {
        memset(&srcFileInfo[i].stat_info,0,sizeof(struct stat));
        srcFileInfo[i].fp = NULL;
        sprintf(srcFileInfo[i].file_name,"//media//ubuntu//bae//%s//%s",file_name,name_arr[i]);
    printf("%s\n",srcFileInfo[i].file_name);
    }

    sprintf(file_out_name,"//media//ubuntu//bae//%s//%s",file_name,"all");
    printf("%s\n",file_out_name);
//    if(fp_read<0)
//    {
//        printf("read file open failed\r\n"); 
//        return -1;
//    }
//
//    stat(file_path_name,&file_stat); 
//
//    printf("fileid:%d,size is:%d\r\n",fp_read,file_stat.st_size);
    pthread_t thread_id;
    //signal(15,handler);
    flag_exit = 0;
    pthread_create(&thread_id,NULL,stat_file_thread,NULL); 
   // while(char_get != 'c' && char_get != 'C')
    //{
    //    char_get =  getchar()&0x00ff;
   // }
    while(flag_exit==0)
    {
        
    }
 
    
    //flag_exit = 1;
    pthread_join(thread_id,NULL);
    //close(fp_read);
    return 0;
}

void * stat_file_thread(void * param)
{
     FILE * fp_write = fopen(file_out_name,"wb+");
     struct stat file_stat_temp;         
     //FILE * fp_read = fopen(file_path_name,"rb");
     unsigned char buff[1024];
     //if(fp_read == NULL)
     //{
     //     printf("open read file failed\r\n");
     //}
     memset(&file_stat,0,sizeof(struct stat));
     while(!flag_exit)
     {
        usleep(1000);
		 if(fp_write == NULL)
		 {
			 fp_write = fopen(file_out_name,"wb+");
		 }
         if(fp_write==NULL)
		 {
			 continue;
		 }			 
        for(int i=0;i<4;i++)
        {
             if(srcFileInfo[i].fp == NULL)
             {
                 srcFileInfo[i].fp = fopen(srcFileInfo[i].file_name,"rb"); 
             }
             if(srcFileInfo[i].fp != NULL)
             {
                 stat(srcFileInfo[i].file_name,&file_stat_temp);
                 if(file_stat_temp.st_size > srcFileInfo[i].stat_info.st_size )
                  {
                      int cnt_to_read = file_stat_temp.st_size - srcFileInfo[i].stat_info.st_size;
                      int cnt_read_once = 0;
                      int cnt_read_ret = 0; 
                      while(cnt_to_read>0)
                      {
                         cnt_read_once = cnt_to_read > 1024?1024:cnt_to_read; 
                         cnt_read_ret = fread(buff,1,cnt_read_once,srcFileInfo[i].fp);
                         if(cnt_read_ret > 0)
                         {
                             fwrite(buff,1,cnt_read_ret,fp_write);
                             cnt_to_read -= cnt_read_ret;
                         }
                         else
                         {
                            break;
                         }
                         
                      }
                      memcpy(&srcFileInfo[i].stat_info,&file_stat_temp,sizeof(struct stat));
                  }

             }

        }
        //stat(file_path_name,&file_stat_temp);
        //printf("file size is:%d\r\n",file_stat_temp.st_size);
        
     }
     //fclose(fp_read);
     for(int i=0;i<4;i++)
     {
		 if(srcFileInfo[i].fp)
		 {
			 fclose(srcFileInfo[i].fp);
			 srcFileInfo[i].fp = NULL;
		 }
       
     }
	 if(fp_write)
	 {
		 fclose(fp_write);
		 fp_write = NULL;
	 }
     
}



